package com.itheima.leetcode;

/**
 * @FileName ListNode
 * @Description
 * @Author Sword
 * @date 2025-03-18
 **/
public class ListNode {

    int val;
    ListNode next;

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder(64);
        sb.append("[");
        ListNode p = this;
        while (p != null) {
            sb.append(p.val);
            if (p.next != null) {
                sb.append(",");
            }
            p = p.next;
        }
        sb.append("]");
        return sb.toString();
    }

    public static ListNode of(int... elements) {
        if (elements == null || elements.length == 0) {
            return null;
        }
        ListNode head = new ListNode(-1, null);
        ListNode p = head;
        for (int element : elements) {
            p.next = new ListNode(element, null);
            p = p.next;
        }
        return head.next;
    }
}
